import { apiRequest } from './api';

export interface NotificationSettings {
  id?: string;
  user_id?: string;
  task_reminders: boolean;
  daily_summary: boolean;
  weekly_review: boolean;
  overdue_alerts: boolean;
  reminder_time: string;
  sound_enabled: boolean;
  vibration_enabled: boolean;
  email_notifications: boolean;
  created_at?: string;
  updated_at?: string;
}

export interface NotificationHistory {
  id: string;
  user_id: string;
  type: 'reminder' | 'overdue' | 'summary' | 'deadline';
  title: string;
  message: string;
  task_id?: string;
  sent_at: string;
  is_read: boolean;
  read_at?: string;
  delivery_status: 'sent' | 'failed' | 'pending';
}

export interface TaskReminder {
  taskId: string;
  title: string;
  message: string;
  type: 'reminder';
  priority: string;
  category: string;
  dueDate: string;
}

export interface OverdueTask {
  id: string;
  title: string;
  description: string;
  dueDate: string;
  priority: string;
  category: string;
  overdueDays: number;
}

export interface DailySummary {
  date: string;
  completed: number;
  pending: number;
  overdue: number;
  upcomingDeadlines: number;
}

class NotificationAPI {
  // 获取通知设置
  async getSettings(): Promise<NotificationSettings> {
    return await apiRequest('/notifications/settings', {
      method: 'GET'
    });
  }

  // 更新通知设置
  async updateSettings(settings: Partial<NotificationSettings>): Promise<NotificationSettings> {
    return await apiRequest('/notifications/settings', {
      method: 'PUT',
      body: JSON.stringify(settings)
    });
  }

  // 检查需要发送的提醒
  async checkReminders(): Promise<TaskReminder[]> {
    return await apiRequest('/notifications/check-reminders', {
      method: 'POST'
    });
  }

  // 获取逾期任务
  async getOverdueTasks(): Promise<OverdueTask[]> {
    return await apiRequest('/notifications/overdue-tasks', {
      method: 'GET'
    });
  }

  // 获取每日总结
  async getDailySummary(): Promise<DailySummary | null> {
    return await apiRequest('/notifications/daily-summary', {
      method: 'GET'
    });
  }

  // 记录通知发送历史
  async recordNotification(notification: {
    type: string;
    title: string;
    message: string;
    task_id?: string;
    sent_at?: string;
  }): Promise<NotificationHistory> {
    return await apiRequest('/notifications/history', {
      method: 'POST',
      body: JSON.stringify(notification)
    });
  }

  // 获取通知历史
  async getNotificationHistory(options: {
    limit?: number;
    offset?: number;
    type?: string;
  } = {}): Promise<NotificationHistory[]> {
    const params = new URLSearchParams();
    if (options.limit) params.append('limit', options.limit.toString());
    if (options.offset) params.append('offset', options.offset.toString());
    if (options.type) params.append('type', options.type);

    const url = `/notifications/history${params.toString() ? `?${params.toString()}` : ''}`;
    return await apiRequest(url, {
      method: 'GET'
    });
  }

  // 标记通知为已读
  async markAsRead(notificationId: string): Promise<NotificationHistory> {
    return await apiRequest(`/notifications/history/${notificationId}/read`, {
      method: 'PATCH'
    });
  }

  // 清理旧的通知历史
  async cleanupHistory(): Promise<{ message: string; deletedCount: number }> {
    return await apiRequest('/notifications/history/cleanup', {
      method: 'DELETE'
    });
  }
}

export const notificationAPI = new NotificationAPI();
export default notificationAPI;
